package com.clothing.controller.app;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.clothing.bean.BulletinJson;
import com.clothing.controller.BaseController;
import com.clothing.entity.Banner;
import com.clothing.entity.Bulletin;
import com.clothing.service.BannerService;
import com.clothing.service.BulletinService;
import com.clothing.utils.AjaxMsg;
import com.clothing.utils.Utils;

/**
 * @ClassName: IndexController 
 * @Description: TODO(首页) 
 * @author Charles 
 * @date 2018年5月8日 上午10:55:43 
 * @version 1.0
 */
@RequestMapping("/app/index")
@Controller("appIndexController")
public class IndexController extends BaseController
{
    @Autowired
    private BannerService bannerService;

    @Autowired
    private BulletinService bulletinService;

    /**
     * @Title: bannerList 
     * @Description: TODO(banner列表) 
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/getBannerList.json", method = RequestMethod.GET)
    @ResponseBody
    public String bannerList()
    {
        List<Banner> banners = bannerService.findBannerList(Boolean.TRUE);
        JSONArray ja = null;

        if (!Utils.isEmpty(banners))
        {
            ja = new JSONArray();

            for (Banner banner : banners)
            {
                JSONObject json = new JSONObject();
                json.put("id", banner.getId());
                json.put("image", banner.getFullImage());
                json.put("url", banner.getUrl());
                ja.add(json);
            }
        }

        return new AjaxMsg(true, ja).toString();
    }

    /**
     * @Title: bulletinList 
     * @Description: TODO(公告列表) 
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/getBulletinList.json", method = RequestMethod.GET)
    @ResponseBody
    public String bulletinList()
    {
        List<Bulletin> bulletins = bulletinService.findBulletinList(Boolean.TRUE);
        JSONArray ja = null;

        if (!Utils.isEmpty(bulletins))
        {
            ja = new JSONArray();

            for (Bulletin bulletin : bulletins)
            {
                BulletinJson json = new BulletinJson();
                json.setId(bulletin.getId());
                json.setContent(bulletin.getContent());
                json.setTitle(bulletin.getTitle());
                json.setCreateDate(bulletin.getCreateDate());
                ja.add(json);
            }
        }

        return new AjaxMsg(true, ja).toString();
    }

    /**
     * @Title: bulletinDetail 
     * @Description: TODO(公告详情) 
     * @param @param id
     * @param @return 设定文件 
     * @return String 返回类型  
     * @throws
     */
    @RequestMapping(value = "/getBulletinDetail.json", method = RequestMethod.GET)
    @ResponseBody
    public String bulletinDetail(Long id)
    {
        Bulletin bulletin = null;

        if (null == id)
        {
            bulletin = bulletinService.findNewBulletin();
        }
        else
        {
            bulletin = bulletinService.find(id);

            if (null == bulletin)
            {
                return getParameterErrorMsg();
            }
        }
        
        BulletinJson json = new BulletinJson();
        json.setId(bulletin.getId());
        json.setContent(bulletin.getContent());
        json.setTitle(bulletin.getTitle());
        json.setCreateDate(bulletin.getCreateDate());

        return new AjaxMsg(true, json).toString();
    }
}
